package com.agentframework.examples.jinhua;

import com.agentframework.core.tools.AgentTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.util.List;

/**
 * @author wangjun
 **/
public class JinhuaTool implements AgentTool {

    private static final Logger logger = LoggerFactory.getLogger(JinhuaTool.class);

    private JinhuaGameState gameState;

    public JinhuaTool() {

    }

    @Tool(description = "发牌给所有玩家")
    public String dealCards(@ToolParam(description = "参与玩家") List<String> players) {
        return gameState.dealCards();
    }

    @Tool(description = "看牌")
    public List<String> viewCards(@ToolParam(description = "玩家") String player) {
        logger.debug(">>>>>玩家 {} 调用 tools viewCards查看底牌", player);

        if (gameState.getPlayer(player).isViewed()) {
            return List.of("玩家已经看牌，不能重复看牌，请直接返回之前的玩家手牌");
        }

        return gameState.viewCards(player);
    }

    @Tool(description = "下注")
    public String bet(@ToolParam(description = "玩家") String player,
                      @ToolParam(description = "下注金额") Integer amount) {
        logger.debug(">>>>>玩家 {} 调用 tools bet进行下注，金额 {}", player, amount);
        return gameState.placeBet(player, amount);
    }

    @Tool(description = "比牌，返回胜者")
    public String compareCards(
            @ToolParam(description = "比牌玩家") String player1,
            @ToolParam(description = "被比牌玩家") String player2) {
        logger.debug(">>>>>玩家 {} 跟 玩家 {} 调用 tools compareCards 进行比牌", player1, player2);
        return gameState.compareCards(player1, player2);
    }

    @Tool(description = "进入下一轮游戏")
    public String nextRound() {
        if (!gameState.nextRound()) {
            return "游戏结束！达到最大轮数。";
        }

        if (gameState.isGameOver()) {
            return "游戏结束！达到最大轮数或者只剩一名玩家。";
        }

        return "进入第 " + gameState.getCurrentRound() + " 轮游戏，共 " + gameState.getTotalRounds() + " 轮。";
    }

    @Tool(description = "弃牌")
    public String fold(@ToolParam(description = "玩家") String playerId) {
        logger.debug(">>>>>玩家 {} 调用 tools fold弃牌", playerId);
        return gameState.playerFold(playerId);
    }

    public void setGameState(JinhuaGameState gameState) {
        this.gameState = gameState;
    }

    /**
     * 获取当前游戏状态
     * @return 游戏状态对象
     */
    public JinhuaGameState getGameState() {
        return gameState;
    }

}
